package com.hhlkeepalive;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.hhlkeepalive.ndkAlive.PushService;
import com.hhlkeepalive.onePx.KeepAliveReceiver;
import com.hhlkeepalive.onePx.ScreenListenerManager;
import com.hhlkeepalive.onePx.ScreenManager;
import com.hhlkeepalive.resident.ResidentServiceFactory;
import com.hhlkeepalive.utils.ActivityManager;

public class MainActivity extends AppCompatActivity {
    private final  static String TAG="KeepAlive";
    private KeepAliveReceiver receiver;
    private ScreenListenerManager mManager;
    private ScreenManager mScreenManager;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //creatPush();
        //creatBroadcastReceiver();
        ActivityManager.addDestroyActivity(this,getClass().getName());
        //SettingUtils.enterWhiteListSetting(this);
        //createSecond();
        createService();



    }

    private void createService() {
        ResidentServiceFactory factory=new ResidentServiceFactory(this);
        factory.creatService();
    }

    private void createSecond() {
        mManager = new ScreenListenerManager(this);
        mManager.register();
        mManager.setOnScreenListener(new ScreenListenerManager.OnScreenListener() {
            @Override
            public void onScreenOn() {
                Log.e(TAG, "onScreenOn: ");
            }

            @Override
            public void onScreenOff() {
                Log.e(TAG, "onScreenOff: ");
                // 开启1像素的Activity
                mScreenManager.startSinglePixelActivity(MainActivity.this);
            }

            @Override
            public void onUserresent() {
                Toast.makeText(MainActivity.this, "onUserresent！", Toast.LENGTH_LONG).show();
                Log.e(TAG, "onUserresent: ");
                // 关闭1像素的Activity
                mScreenManager.finishSinglePixelActivity();
            }
        });
        mScreenManager = ScreenManager.getInstance(getApplicationContext());
    }

    private void creatBroadcastReceiver() {
        receiver=new KeepAliveReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(receiver,filter);
        Log.e(TAG,"MainActivity--->广播接收器已开启");

    }

    private void creatPush() {
        Intent intent=new Intent(this,PushService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.destroyActivity(getClass().getName());
        mManager.unRegister();
    }
}
